using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class WindowClass : IElementContainer
    {
        private string name;
        public string Name { get { return name; } set { name = value; } }

        private string frame;
        public string Frame { get { return frame; } set { frame = value; } }

        private string dataSource;
        public string DataSource { get { return dataSource; } set { dataSource = value; } }

        private bool sharable = false;
        public bool Sharable { get { return sharable; } set { sharable = value; } }

        private string shareAs;
        public string ShareAs { get { return shareAs; } set { shareAs = value; } }

        private bool playerControl = false;
        public bool PlayerControl { get { return playerControl; } set { playerControl = value; } }

        private bool softClose = false;
        public bool SoftClose { get { return softClose; } set { softClose = value; } }

        private bool noDelete = false;
        public bool NoDelete { get { return noDelete; } set { noDelete = value; } }

        private string minimizeIcon;
        public string MinimizeIcon { get { return minimizeIcon; } set { minimizeIcon = value; } }

        private WindowClassTooltip toolTip;
        public WindowClassTooltip Tooltip { get { return toolTip; } set { toolTip = value; } }

        private WindowClassPlacement placement;
        public WindowClassPlacement Placement { get { return placement; } set { placement = value; } }

        private SizeLimits sizeLimits;
        public SizeLimits Size { get { return sizeLimits; } set { sizeLimits = value; } }

        private Script script;
        public Script Scripts { get { return script; } set { script = value; } }

        private SheetData sheetData;
        public SheetData SheetData { get { return sheetData; } set { sheetData = value; } }

        public WindowClass(Element element)
        {
            sourceElement = element.Copy();

            Attribute nameAttribute = sourceElement.FindAttribute("name");
            if (nameAttribute != null)
            {
                name = nameAttribute.Value.Substring(0);
            }

            Element subElement = sourceElement.FindFirstElement("frame", false);
            if (subElement != null)
            {
                frame = subElement.Value.Substring(0);
            }

            subElement = sourceElement.FindFirstElement("datasource", false);
            if (subElement != null)
            {
                dataSource = subElement.Value.Substring(0);
            }

            subElement = sourceElement.FindFirstElement("sharable", false);
            if (subElement != null)
            {
                sharable = true;
                Element shareAsElement = subElement.FindFirstElement("shareas", false);
                if (shareAsElement != null)
                {
                    shareAs = shareAsElement.Value.Substring(0);
                }
            }

            subElement = sourceElement.FindFirstElement("playercontrol", false);
            if (subElement != null)
            {
                playerControl = true;
            }

            subElement = sourceElement.FindFirstElement("softclose", false);
            if (subElement != null)
            {
                softClose = true;
            }

            subElement = sourceElement.FindFirstElement("nodelete", false);
            if (subElement != null)
            {
                noDelete = true;
            }

            subElement = sourceElement.FindFirstElement("minimize", false);
            if (subElement != null)
            {
                minimizeIcon = subElement.Value.Substring(0);
            }

            subElement = sourceElement.FindFirstElement("tooltip", false);
            if (subElement != null)
            {
                toolTip = new WindowClassTooltip(subElement);
            }

            subElement = sourceElement.FindFirstElement("placement", false);
            if (subElement != null)
            {
                placement = new WindowClassPlacement(subElement);
            }

            subElement = sourceElement.FindFirstElement("sizelimits", false);
            if (subElement != null)
            {
                sizeLimits = new SizeLimits(subElement);
            }

            subElement = sourceElement.FindFirstElement("script", false);
            if (subElement != null)
            {
                script = new Script(subElement);
            }

            subElement = sourceElement.FindFirstElement("sheetdata", false);
            if (subElement != null)
            {
                sheetData = new SheetData(subElement);
            }

        }

        #region IElementContainer Members

        private Element sourceElement;

        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            // TODO: Commit windowclass
        }

        #endregion
    }
}
